﻿using NetCore8Demo.Models.Communication.SerialPortFolder;
using NetCore8Demo.Models.Param;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Views.Param;

namespace NetCore8Demo.Views.Communication.SerialPortFolder
{
    public partial class EditSerialPortForm : Form
    {
        public EditSerialPortForm()
        {
            InitializeComponent();
        }


        #region 变量定义
        List<SerialPortInfo> _serialPortInfoList = new();
        int _comIndex;
        private int _startUcIndex = 0;
        public int SerialPortUcCount = 4;//右边显示几个

        #endregion

        #region 初始化
        private void EditSerialPortForm_Load(object sender, EventArgs e)
        {
            LoadSerialPortInfoList();
            InitSerialPortUc();
        }

        public void InitSerialPortUc()
        {
            serialPortSetUc1.ClearControlData();
            serialPortSetUc2.ClearControlData();
            serialPortSetUc3.ClearControlData();
            serialPortSetUc4.ClearControlData();

            int endIndex = Math.Min(_startUcIndex + SerialPortUcCount, _serialPortInfoList.Count);
            for (int i = _startUcIndex; i < endIndex; i++)
            {
                switch (i - _startUcIndex)
                {
                    case 0:
                        serialPortSetUc1.Init(_serialPortInfoList[i].Id);
                        break;
                    case 1:
                        serialPortSetUc2.Init(_serialPortInfoList[i].Id);
                        break;
                    case 2:
                        serialPortSetUc3.Init(_serialPortInfoList[i].Id);
                        break;
                    case 3:
                        serialPortSetUc4.Init(_serialPortInfoList[i].Id);
                        break;
                }
            }
        }

        #endregion

        #region 页面按钮

        private void NewBtn_Click(object sender, EventArgs e)
        {
            List<ParamInfo> paramList = new();
            int tempIndex = 0;
            if (_serialPortInfoList.Count > 0)
            {
                tempIndex = _serialPortInfoList.Max(t => t.Index) + 1;
            }
            paramList.Add(new ParamInfo("序号", tempIndex + ""));


            ParamInfo param = new ParamInfo
            {
                LabelName = "串口号",
                OldValue = "",
                ParamType = ParamType.ComboBox,
                ValueList = StaticSerialPort.GetAllPortName(),
            };
            paramList.Add(param);
            paramList.Add(new ParamInfo("名称"));



            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }
            SerialPortInfo serialPortInfo = new SerialPortInfo
            {
                Id = Guid.NewGuid()
            };
            int.TryParse(paramList[0].NewValue, out serialPortInfo.Index);
            serialPortInfo.PortName = paramList[1].NewValue;
            serialPortInfo.Name = paramList[2].NewValue;

            _serialPortInfoList.Add(serialPortInfo);
            SavaPlcAddressInfoList();
        }

        private void DelBtn_Click(object sender, EventArgs e)
        {
            if (SerialPortInfoListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择");
                return;
            }
            _comIndex = SerialPortInfoListView.SelectedIndices[0];
            string id = this.SerialPortInfoListView.Items[_comIndex].SubItems[0].Text.Trim();

            _serialPortInfoList.RemoveAll(p => p.Id == Guid.Parse(id));

            SavaPlcAddressInfoList();
            InitSerialPortUc();
        }

        private void ReFreshBtn_Click(object sender, EventArgs e)
        {
            LoadSerialPortInfoList();
            InitSerialPortUc();
        }

       

        private void SerialPortInfoListView_DoubleClick(object sender, EventArgs e)
        {
            _comIndex = SerialPortInfoListView.SelectedIndices[0];
            string id = this.SerialPortInfoListView.Items[_comIndex].SubItems[0].Text.Trim();
            SerialPortInfo? oldInfo = _serialPortInfoList.FirstOrDefault(p => p.Id == Guid.Parse(id));
            if (oldInfo == null) { return; }

            List<ParamInfo> paramList =
            [
                new("序号", oldInfo.Index + ""),
                new ParamInfo("名称", oldInfo.Name),
                new ParamInfo("发送数据", oldInfo.SendData)
            ];

            ParamInfo param = new ParamInfo();
            param.LabelName = "是否启用";
            param.OldValue = oldInfo.IsInUse.ToString();
            param.ParamType = ParamType.YesOrNo;
            paramList.Add(param);


            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();
            if (!basicParamForm.SureFlag)
            {
                return;
            }

            foreach (var item in _serialPortInfoList)
            {
                if (item.Id == Guid.Parse(id))
                {
                    int.TryParse(paramList[0].NewValue, out item.Index);
                    item.Name = paramList[1].NewValue;
                    item.SendData = paramList[2].NewValue;
                    bool.TryParse(paramList[3].NewValue, out item.IsInUse);
                    break;
                }
            }
            SavaPlcAddressInfoList();
        }

        private void SerialPortInfoListView_Click(object sender, EventArgs e)
        {

            int tempIndex = SerialPortInfoListView.SelectedIndices[0];
            if (tempIndex / SerialPortUcCount == _startUcIndex / SerialPortUcCount)
            {
                return;
            }
            _startUcIndex = SerialPortInfoListView.SelectedIndices[0];
            _startUcIndex = (_startUcIndex / SerialPortUcCount) * SerialPortUcCount;


            InitSerialPortUc();
        }


        #endregion

        #region 方法

        public void SavaPlcAddressInfoList()
        {
            //升序排列
            _serialPortInfoList = _serialPortInfoList.OrderBy(s => s.Index).ToList();
            StaticSerialPort.SavaSerialPortInfoList(_serialPortInfoList);

            LoadSerialPortInfoList();
        }

        public void LoadSerialPortInfoList()
        {
            _serialPortInfoList = StaticSerialPort.LoadSerialPortInfoList();

            this.SerialPortInfoListView.Items.Clear();
            foreach (var item in _serialPortInfoList)
            {

                ListViewItem lvi = new ListViewItem
                {
                    Text = item.Id + ""
                };
                lvi.SubItems.Add(item.Index + "");
                lvi.SubItems.Add(item.Name);
                lvi.SubItems.Add(item.IsInUse ? "启用" : "");
                this.SerialPortInfoListView.Items.Add(lvi);
            }
        }

        #endregion

       
    }
}
